<template>
  <div>
    <el-button type="primary" @click="add">添加分类</el-button>
    <!-- 表格 -->
    <el-table
      :data="getCategories"
      style="width: 100%; margin-bottom: 20px"
      row-key="cat_id"
      border
      default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column label="#" type="index"> </el-table-column>
      <el-table-column label="分类名称" prop="cat_name"> </el-table-column>
      <el-table-column label="是否有效" prop="cat_deleted">
        <template slot-scope="scope">
          <el-button
            type="success"
            size=" mini"
            v-if="scope.row.cat_deleted == false"
            icon="el-icon-check"
            circle
          ></el-button>
          <el-button
            type="danger"
            size=" mini"
            v-else
            icon="el-icon-check"
            circle
          ></el-button>
        </template>
      </el-table-column>
      <el-table-column label="排序" prop="cat_level">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.cat_level == 0">一级</el-tag>
          <el-tag v-else-if="scope.row.cat_level == 1">二级</el-tag>
          <el-tag v-else>三级</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.cat_id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page.pagenum"
      :page-sizes="[1, 5, 10, 15]"
      :page-size="page.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 添加对话框 -->
    <el-dialog title="添加分类" :visible.sync="dialogVisible" width="50%">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="ruleForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类" prop="cat_level">
          <!-- 级联选择器 -->
          <!-- change  级联选择器发生变化就会触发 -->
          <el-cascader
            @change="handleChange"
            v-model="val"
            :options="getCategories"
            :props="{
              checkStrictly: true,
              expandTrigger: 'hover',
              label: 'cat_name',
              value: 'cat_id',
            }"
            clearable
          ></el-cascader>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">添加商品</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 编辑对话框 -->
    <el-dialog title="修改分类" :visible.sync="dialogVisibleedit" width="50%">
      <el-form
        :model="ruleFormedit"
        :rules="rulesedit"
        ref="ruleFormedit"
        label-width="100px"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="ruleFormedit.cat_name"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmitedit">确定</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import {
  getCategories,
  getaddCategories,
  geteditcategories,
  getdelcategories,
} from "../../utils/api";
export default {
  data() {
    return {
      cat_id: "", //编辑提交用到的id
      val: [], //级联选中的id
      dialogVisibleedit: false, //修改分类模态框的显示与隐藏
      options: {}, //级联选择器内容
      ruleFormedit: {
        //修改分表单
        cat_name: "", //分类名称
      },
      ruleForm: {
        //添加表单
        cat_name: "", //分类名称
        cat_level: "", //父级分类
      },
      rulesedit: {
        //修改分类的表单校验
        cat_name: [
          { required: true, message: "请输分类名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
      },
      rules: {
        cat_name: [
          { required: true, message: "请输分类名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
      },
      dialogVisible: false, //添加对话框的显示与隐藏
      total: 0,
      getCategories: [], //商品分类数据
      page: {
        pagenum: 1,
        pagesize: 5,
      },
    };
  },
  methods: {
    onSubmitedit() {
      //修改模态框中的确定按钮
      geteditcategories({
        cat_name: this.ruleFormedit.cat_name,
        id: this.cat_id,
      }).then((res) => {
        console.log(res);
        if (res.data.meta.status == 200) {
          this.$message({
            type: "success", // success error warning
            message: res.data.meta.msg,
            duration: 2000,
          });
        }
        this.rander();
        this.dialogVisibleedit = false;
      });
    },
    onSubmit() {
      //添加商品确定按钮
      let obj = {
        cat_pid:
          this.val.length == 0
            ? 0
            : this.val.length >= 2
            ? this.val[1]
            : this.val[this.val.length - 1],
        cat_name: this.ruleForm.cat_name,
        cat_level: this.val.length >= 2 ? 2 : this.val.length,
      };
      console.log(obj);
      getaddCategories(obj).then((res) => {
        if (res.data.meta.status == 200) {
          this.$message({
            type: "success", // success error warning
            message: res.data.meta.msg,
            duration: 2000,
          });
        }
        console.log(res);
        this.rander();
        this.dialogVisible = false;
      });
    },
    handleChange() {
      //  console.log(this.val);
    },
    add() {
      //添加按钮
      this.dialogVisible = true;
    },
    handleEdit(row) {
      //编辑回填
      console.log(row);
      this.dialogVisibleedit = true;
      this.ruleFormedit.cat_name = row.cat_name;
      this.cat_id = row.cat_id;
    },
    handleDelete(id) {
      //删除
      console.log(id);
      getdelcategories(id).then((res) => {
        console.log(res);
        if (res.data.meta.status == 200) {
          this.$message({
            type: "success", // success error warning
            message: res.data.meta.msg,
            duration: 2000,
          });
        }
        this.rander();
      });
    },
    handleSizeChange(val) {
      //每页几条数据
      this.page.pagesize = val;
      this.rander();
    },
    handleCurrentChange(val) {
      //当前页码
      this.page.pagenum = val;
      this.rander();
    },
    rander() {
      getCategories(this.page).then((res) => {
        //请求商品分类数据
        console.log(res);
        this.getCategories = res.data.data.result;
        this.total = res.data.data.total;
      });
    },
  },
  created() {
    this.rander();
  },
};
</script>